﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Mvc.Utility; 
using Wedo.Recognition.Web.Controllers;
using Wedo.Recognition.Service;
using Wedo.Mvc.Pager;
using Wedo.Recognition.Domain;
using Wedo.Domain.Models; 
using Wedo.Recognition.Repository;

namespace Wedo.IPRP.Web.Controllers
{
    public class ParamListController : EntityKeyController<SysParam, string,SearchModel>
    {
        private SysParamService service;
        private IUnitWork _dbContext; 

        public ParamListController(IUnitWork dbContext)
        {
            _dbContext = dbContext;
            service = new SysParamService(_dbContext);
        }

        protected override PagedList<SysParam> GetSearchPagedList(SearchModel where)
        {
           return service.ToPagedList(where);
        }

        protected override void DeleteModel(string id)
        {
            service.Remove(id);
        }

        protected override SysParam FindById(string Id)
        {
            if (Id != null && Id.Length > 0)
            {
                var parentID = Id.Substring(0, Id.Length - 3);
                var parent = service.GetById(parentID);
                if (parent != null)
                    ViewData["ParentText"] = parent.ParamText;
            }
            return service.GetById(Id);
        }

        public override ActionResult Create()
        {
            ViewData["Parent"] = service.GetCategory("");

            return base.Create();
        }

        protected override void Insert(SysParam model)
        {
            service.Add(model, Request.Form["Parent"]);
        }


        protected override void UpdateModel(SysParam model)
        {
            service.Update(model);
        }

        public override ActionResult Create(SysParam model)
        { 
            ModelState.Remove("Id");

            return base.Create(model);
        }
    }
}
